// SettingPage.cpp : implementation file
//

#include "stdafx.h"
#include "FastTab.h"
#include "SettingPage.h"
#include "SettingManager.h"

// SettingPage dialog

IMPLEMENT_DYNAMIC(SettingPage, CPropertyPage)

SettingPage::SettingPage()
	: CPropertyPage(SettingPage::IDD)
	, mAutoRun(false)
{
	
}

SettingPage::~SettingPage()
{
}

void SettingPage::DoDataExchange(CDataExchange* pDX)
{
	CPropertyPage::DoDataExchange(pDX);
	Properties& p = SettingManager::Instance()->GetModifyProperties();
	DDX_Check(pDX, IDC_AUTO_RUN, mAutoRun);
	DDX_Check(pDX, IDC_IS_NEXT_ALWAYS, p.IsShowNextAlways);
	DDX_Check(pDX, IDC_IM_LISTEN, p.IsImListen);
	DDX_Check(pDX, IDC_CHECK_NOT_FIX_WITH_FONT, p.IconNotFixWithFont);
	DDX_Text(pDX, IDC_EDIT_ICON_SIZE, p.IconSize);
	DDV_MinMaxInt(pDX, p.IconSize, 4, 128);
}


BEGIN_MESSAGE_MAP(SettingPage, CPropertyPage)
	ON_COMMAND(IDC_AUTO_RUN, OnChange)
	ON_COMMAND(IDC_IS_NEXT_ALWAYS, OnChange)
	ON_COMMAND(IDC_IM_LISTEN, OnChange)
	ON_BN_CLICKED(IDC_SETTING_FONT, &SettingPage::OnBnClickedSettingFont)
	ON_BN_CLICKED(IDC_CHECK_NOT_FIX_WITH_FONT, &SettingPage::OnBnClickedCheckNotFixWithFont)
END_MESSAGE_MAP()

BOOL SettingPage::OnInitDialog()
{
	MakeCaption(IDC_AUTO_RUN);
	MakeCaption(IDC_IS_NEXT_ALWAYS);
	MakeCaption(IDC_IM_LISTEN);
	mAutoRun = SettingManager::Instance()->IsAutoRun();
	SettingManager::Instance()->ReloadProperties();
	RefreshIconSizeCtrl();
	return __super::OnInitDialog();
}

void SettingPage::MakeCaption( UINT id )
{
	CString str;
	str.LoadString(id);
	SetDlgItemText(id, str);
}

void SettingPage::OnOK()
{
	SettingManager::Instance()->SaveProperties();
	__super::OnOK();
}

void SettingPage::OnChange()
{
	SetModified();
}
// SettingPage message handlers


void SettingPage::OnBnClickedSettingFont()
{
	Properties& p = SettingManager::Instance()->GetModifyProperties();
	LOGFONT logFont;
	logFont.lfHeight = p.lfHeight;
	logFont.lfWidth = p.lfWidth;
	logFont.lfEscapement = p.lfEscapement;
	logFont.lfOrientation = p.lfOrientation;
	logFont.lfWeight = p.lfWeight;
	logFont.lfItalic = p.lfItalic;
	logFont.lfUnderline = p.lfUnderline;
	logFont.lfStrikeOut = p.lfStrikeOut;
	logFont.lfCharSet = p.lfCharSet;
	logFont.lfOutPrecision = p.lfOutPrecision;
	logFont.lfClipPrecision = p.lfClipPrecision;
	logFont.lfQuality = p.lfQuality;
	logFont.lfPitchAndFamily = p.lfPitchAndFamily;
	_tcscpy(logFont.lfFaceName, p.lfFaceName.c_str());

	CFontDialog fontDialog(&logFont, CF_INITTOLOGFONTSTRUCT, NULL, this);
	if (fontDialog.DoModal() == IDOK)
	{
		fontDialog.GetCurrentFont(&logFont);
		p.lfHeight = logFont.lfHeight;
		p.lfWidth = logFont.lfWidth;
		p.lfEscapement = logFont.lfEscapement;
		p.lfOrientation = logFont.lfOrientation;
		p.lfWeight = logFont.lfWeight;
		p.lfItalic = logFont.lfItalic;
		p.lfUnderline = logFont.lfUnderline;
		p.lfStrikeOut = logFont.lfStrikeOut;
		p.lfCharSet = logFont.lfCharSet;
		p.lfOutPrecision = logFont.lfOutPrecision;
		p.lfClipPrecision = logFont.lfClipPrecision;
		p.lfQuality = logFont.lfQuality;
		p.lfPitchAndFamily = logFont.lfPitchAndFamily;
		p.lfFaceName = logFont.lfFaceName;
	}
	// TODO: Add your control notification handler code here
}

void SettingPage::OnBnClickedCheckNotFixWithFont()
{
	UpdateData(TRUE);
	RefreshIconSizeCtrl();
}

void SettingPage::RefreshIconSizeCtrl()
{
	
	Properties& p = SettingManager::Instance()->GetModifyProperties();
	GetDlgItem(IDC_EDIT_ICON_SIZE)->EnableWindow(!p.IconNotFixWithFont);
}
